package com.zygame.sdk.view.web;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.zygame.sdk.utils.http.NetHttpUtil;
import com.zygame.sdk.utils.http.RequestModel;
import com.zygame.sdk.utils.http.NetHttpUtil.DataCallback;
import com.zygame.sdk.utils.res.FYResource;
import com.zygame.sdk.utils.res.ResourceUtil;
import com.zygame.sdk.utils.ui.ExitAppUtils;
import com.zygame.sdk.view.CustomProgressDialog;

/**
 * Created with IntelliJ IDEA. Copyright: Copyright (c) 2013 User: xiaoming.yuan
 * 13-9-17 Time: 上午10:51 To change this template use File | Settings | File
 * Templates.
 */
public abstract class BaseActivity extends FragmentActivity implements OnClickListener {
//	private static final String TAG = "BaseActivity";

	protected Activity mBaseActivity;

	protected CustomProgressDialog mProgressDialog;

	protected int mOrientation;

	/**
	 * 公有控件
	 */
	protected View contentView; 
	// protected TextView tv_top_title;
	protected Button btn_nav_top_left;
	protected Button btn_nav_top_right;
	protected ImageButton imBtn_nav_top_right;
	protected LinearLayout ll_content;
	protected RelativeLayout rl_base_topBar;
	protected RadioGroup rg_base_bottom;
	protected RadioGroup ll_top_middle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		if(CmGameSDK.getInstance().getOrientation()){
//			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//		}else {
//			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);            //竖屏
//		}

		setContentView(ResourceUtil.getLayoutId(this, FYResource.layout.fy_ly_activity_base));
  
		mBaseActivity = this;
		initView();  
		ExitAppUtils.getInstance().addActivity(this);
	}

	/**
	 * 设置监听
	 * 
	 * @return
	 */
	protected void setListener(View[] views) {
		int length;
		if (views != null && (length = views.length) != 0) {
			for (int i = 0; i < length; i++) {
				views[i].setOnClickListener(this);  
			}
		}
	};

	/**
	 * @return void 返回类型
	 * @Title: initView(初始化组件)
	 * @author xiaoming.yuan
	 * @data 2013-7-12 下午9:13:35
	 */
	private void initView() {
		ll_content = (LinearLayout) findViewById(ResourceUtil.getId(this, FYResource.id.ll_content));
		// tv_top_title = (TextView) findViewById(ResourceUtil.getId(this,
		// FYResource.id.tv_top_title));
		btn_nav_top_right = (Button) findViewById(ResourceUtil.getId(this, FYResource.id.btn_nav_top_right));
		
		imBtn_nav_top_right = (ImageButton) findViewById(ResourceUtil.getId(this, "imBtn_nav_top_right"));
		
		// btn_nav_top_right.setOnClickListener(this);
		btn_nav_top_left = (Button) findViewById(ResourceUtil.getId(this, FYResource.id.btn_nav_top_left));
		rl_base_topBar = (RelativeLayout) findViewById(ResourceUtil.getId(this, FYResource.id.rl_base_topBar));
		rg_base_bottom = (RadioGroup) findViewById(ResourceUtil.getId(this, "rg_base_bottom"));

		ll_top_middle = (RadioGroup) findViewById(ResourceUtil.getId(this, "ll_top_middle"));
//		hideTopBar();
	

		btn_nav_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// finish();
				leftClick();
			}
		});
		loadViewLayout();
		findViewById();
		setListener();
		processLogic();
	}

	protected void leftClick() {
		this.finish();
	};

	protected void hideTopBar() {
		if (rl_base_topBar != null) {
			rl_base_topBar.setVisibility(View.GONE);
		}
	}

	protected void showTopBar() {
		if (rl_base_topBar != null) {
			rl_base_topBar.setVisibility(View.VISIBLE);
		}
	}

	protected void hideBottom() {
		if (rg_base_bottom != null) {
			rg_base_bottom.setVisibility(View.GONE);
		}
	}

	protected void showBottom() {
		if (rg_base_bottom != null) {
			rg_base_bottom.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * @author xiaoming.yuan
	 * @date 2013年9月28日 下午4:23:48
	 * @param layoutResName
	 */
	public void setContentView(String layoutResName) {
		super.setContentView(ResourceUtil.getLayoutId(this, layoutResName));
	}

	/***
	 * 设置内容区域
	 * 
	 * @author K
	 * @param resId
	 *            资源文件ID
	 */
	public void setContentLayout(int resId) {
		LayoutInflater inflater = getLayoutInflater();
		contentView = inflater.inflate(resId, null);
		// LayoutParams layoutParams = new
		// LayoutParams(LayoutParams.FILL_PARENT,
		// LayoutParams.FILL_PARENT);
		// contentView.setLayoutParams(layoutParams);
		// contentView.setBackgroundDrawable(null);
		if (null != ll_content) {
			ll_content.addView(contentView);
		}

		// ViewGroup decor = (ViewGroup) this.getWindow().getDecorView();
		// ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
		// layout.setContentView(decorChild,this);

	}

	/**
	 * @author xiaoming.yuan
	 * @date 2013年9月28日 下午4:25:48
	 * @param viewName
	 * @return
	 */
	public View findViewById(String viewName) {
		return super.findViewById(ResourceUtil.getId(this, viewName));
	}

	/**
	 * 找到控件
	 * 
	 * @param id
	 * @return
	 */
	public View findViewByName(String resName) {
		return findViewById(ResourceUtil.getId(this, resName));
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mBaseActivity = null;
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
		ExitAppUtils.getInstance().delActivity(this);
	}

	/**
	 * @return String 返回类型
	 * @Title: passport(获取用户名)
	 * @author xiaoming.yuan
	 * @data 2013-8-14 下午12:33:59
	 */
	protected String getPassport() {
		/*
		 * UserModel userModel = getUserData(); if (null != userModel && null !=
		 * userModel.getUserName()) { return userModel.getUserName(); }
		 */
		return "";
	}

	/**
	 * @return Integer 返回类型
	 * @Title: uid(获取用户名的唯一标示)
	 * @author xiaoming.yuan
	 * @data 2013-8-14 下午12:33:59
	 */
	protected Integer getUid() {
		/*
		 * UserModel userModel = getUserData(); if (null != userModel && null !=
		 * userModel.getUserName()) { return userModel.getUid(); }
		 */
		return null;
	}

	/**
	 * @return String 返回类型
	 * @Title: password(获取用户密码)
	 * @author xiaoming.yuan
	 * @data 2013-8-14 下午12:33:26
	 */
	protected String getPassword() {
		/*
		 * UserModel userModel = getUserData(); if (null != userModel && null !=
		 * userModel.getPassword()) { return userModel.getPassword(); }
		 */
		return "";
	}

	/**
	 * @author xiaoming.yuan
	 * @date 2013年10月18日 下午6:58:56
	 * @return
	 */
	/*
	 * protected UserModel getUserData() { UserModel userModel =
	 * SplusPayManager.getInstance().getUserModel(); if (userModel == null) {
	 * userModel = AppUtil.getUserData(); } return userModel; }
	 */

	/**
	 * 隐藏软键盘
	 */
	protected void hideSoftInput(Context context) {
		InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		// manager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
		View view = getCurrentFocus();
		if (view != null) {
			manager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	/**
	 * Title: onResume Description:
	 * 
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
//		_65PayManager.getInstance().onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
//		_65PayManager.getInstance().onPause(this);
		hideSoftInput(this);
	}

	// 验证密码是否格式良好
	protected boolean isPasswordCorrect(String password) {
		if (!TextUtils.isEmpty(password)) {
			Pattern pattern = Pattern.compile("^[0-9a-zA-Z_]{6,20}$");
			Matcher matcher = pattern.matcher(password);
			return matcher.find();
		}
		return false;
	}

	// 用户是否格式良好
	protected boolean isUserNameCorrect(String userName) {
		if (!TextUtils.isEmpty(userName)) {
			Pattern pattern = Pattern.compile("^[0-9a-zA-Z_]{4,20}$");
			Matcher matcher = pattern.matcher(userName);
			return matcher.find();
		}
		return false;
	}

	/**
	 * @return void 返回类型
	 * @Title: showProgressDialog(设置进度条)
	 * @author xiaoming.yuan
	 * @data 2013-7-12 下午10:09:36
	 */
	protected void showProgressDialog() {
		if ((!isFinishing()) && this.mProgressDialog == null) {
			this.mProgressDialog = new CustomProgressDialog(this);
		}
		// 设置ProgressDialog 的进度条style
		this.mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		// this.mProgressDialog.setTitle("提示");
		this.mProgressDialog.setMessage("加载中...");
		// 设置ProgressDialog 的进度条是否不明确
		this.mProgressDialog.setIndeterminate(false);
		// 设置ProgressDialog 的进度条是否不明确
		this.mProgressDialog.setCancelable(false);
		this.mProgressDialog.setCanceledOnTouchOutside(false);
		this.mProgressDialog.show();
	}

	/**
	 * @return void 返回类型
	 * @Title: closeProgressDialog(关闭进度条)
	 * @author xiaoming.yuan
	 * @data 2013-7-12 下午10:09:30
	 */
	protected void closeProgressDialog() {
		if (this.mProgressDialog != null && this.mProgressDialog.isShowing())
			this.mProgressDialog.dismiss();
	}

	/**
	 * @return void 返回类型
	 * @Title: loadViewLayout(设置布局文件)
	 * @author xiaoming.yuan
	 * @data 2013-7-12 下午10:09:09
	 */
	protected abstract void loadViewLayout();

	/**
	 * @return void 返回类型
	 * @Title: findViewById(实例化组件)
	 * @author xiaoming.yuan
	 * @data 2013-8-5 下午10:15:03
	 * @param:
	 */
	protected abstract void findViewById();

	/**
	 * @return void 返回类型
	 * @Title: setListener(设置控件监听)
	 * @author xiaoming.yuan
	 * @data 2013-7-12 下午10:08:51
	 */
	protected abstract void setListener();

	/**
	 * @return void 返回类型
	 * @Title: processLogic(处理一些其余的操作)
	 * @author xiaoming.yuan
	 * @data 2013-7-12 下午10:09:01
	 */
	protected abstract void processLogic();

	protected <T> void getDataFromServer(RequestModel mRequestModel, DataCallback<T> callBack) {

		if (setIsShowProgressDialog()) {
			if (mProgressDialog == null || !mProgressDialog.isShowing()) {
				showProgressDialog();
			}
		}
		NetHttpUtil.getDataFromServerPOST(BaseActivity.this, mRequestModel, callBack);
	}

	/**
	 * @Title: isGetDeviceno(是否获取deviceno)
	 * @author xiaoming.yuan
	 * @data 2013-9-26 上午9:29:22
	 * @return boolean 返回类型
	 */
	protected boolean isGetDeviceno() {
		return true;
	}

	/***
	 * @Title: setIsShowProgressDialog(是否显示对话框)
	 * @author xiaoming.yuan
	 * @data 2013-10-3 下午4:09:53
	 * @return boolean 返回类型
	 */
	public boolean setIsShowProgressDialog() {
		return true;
	}

	/**
	 * @return String 返回类型
	 * @Title: getDeviceno(获取设备标识)
	 * @author xiaoming.yuan
	 * @data 2013-8-10 下午4:55:01
	 */
	protected String getDeviceno() {
		// return
		// SharedPreferencesHelper.getInstance().getdevicenoPreferences(mBaseActivity);
		return null;
	}
	
	/**
	 *  隐藏右边button
	 */
	public void hideRightButton(){
		btn_nav_top_right.setVisibility(View.GONE);
	}
}
